//
//  JKLaunchViewController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/2.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKLaunchViewController: UIViewController {
    
    var jumpTo: String?
    
    // MARK: - 属性
    
    private lazy var coverView: UIView = {
        
        let coverView = UIView(frame: JKScreenBounds)
        
        coverView.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        coverView.isUserInteractionEnabled = false
        self.view.addSubview(coverView)
        
        return coverView
    }()
    
    private lazy var launchImageView = UIImageView(frame: JKScreenBounds)
    
    private lazy var sourceLabel: UILabel = {
        
        let sourceLabel = UILabel(frame: CGRect(x: (JKScreenW-200)*0.5, y: (JKScreenH-133), width: 200, height: 30))//[[UILabel alloc] initWithFrame:(CGRectMake((JKScreenW-200)/2, (JKScreenH-133), 200, 30))];
        
        sourceLabel.font = UIFont.systemFont(ofSize: 12)
        sourceLabel.textAlignment = NSTextAlignment.center;
        sourceLabel.textColor = JKColor(237, 237, 237)
        sourceLabel.backgroundColor = UIColor.clear
        sourceLabel.shadowColor = UIColor.darkGray
        sourceLabel.shadowOffset = CGSize(width: 1, height: 0)
        
        return sourceLabel
    }()
    
    private lazy var bottomView : UIView = {
        
        let bottomView = UIView(frame: CGRect(x: 0, y: JKScreenH, width: JKScreenW, height: 100))
        bottomView.backgroundColor = JKColor(24, 24, 24)
        
        return bottomView
    }()
    
    private lazy var iconImgView : UIImageView = {
        
        let iconImgView = UIImageView(frame: CGRect(x: 20, y: (self.bottomView.frame.size.height - 60) * 0.5, width: 50, height: 50))
        iconImgView.image = UIImage(named: "AD_Icon_Border")
        
        return iconImgView
    }()
    
    private lazy var iconView : IconView = {
        
        let iconView = IconView(frame: self.iconImgView.bounds)
        iconView.backgroundColor = UIColor.clear
        
        return iconView
    }()
    
    
    // MARK: - 初始化
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        UIApplication.shared.setStatusBarHidden(true, with: UIStatusBarAnimation.none)
        
        setup()
        
        loadLaunchImage()
    }
    
    private func loadLaunchImage() {
        
        launchImageView.image = UIImage(named: "launch_img_bak.jpg")
        sourceLabel.text = "啊哦，启动图api又更新了"
    }
    
    private func setup() {
        
        //加载图片,并加入view中
        view.insertSubview(launchImageView, at: 0)
        
        //初始化版权Label
        view.addSubview(sourceLabel)
        
        //初始化底部视图
        setupBottomView()
        
        view.bringSubview(toFront: coverView)
    }
    
    private func setupBottomView() {
        
        //底部容器视图
        view.addSubview(bottomView)
        
        //icon图片的imageView
        bottomView.addSubview(iconImgView)
        
        // 要画出来的iconView
        iconImgView.addSubview(iconView)
        
        //标题
        let titleLabel = UILabel(frame: CGRect(x: iconImgView.frame.origin.x + iconImgView.frame.size.width + 15, y: iconImgView.frame.origin.y, width: 200, height: 25))
        titleLabel.text = "知乎日报"
        titleLabel.textAlignment = NSTextAlignment.left
        titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        titleLabel.textColor = JKColor(196, 200, 208)
        bottomView.addSubview(titleLabel)
        
        // 子标题
        let subTitleLabel = UILabel(frame: CGRect(x: titleLabel.frame.origin.x, y: titleLabel.frame.maxY, width: 200, height: 25))
        subTitleLabel.text = "每天三次，每次七分钟"
        subTitleLabel.textAlignment = NSTextAlignment.left
        subTitleLabel.font = UIFont.boldSystemFont(ofSize: 14)
        subTitleLabel.textColor = JKColor(99, 102, 105)
        bottomView.addSubview(subTitleLabel)
    }
    
    //常量
    private let animationDuration: CGFloat = 4.0; //动画的时间
    private let scale: CGFloat = 1.15;            //放大倍数
    
    
    private var link: CADisplayLink?
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        UIView.animate(withDuration: 0.5, animations: {
            
            self.bottomView.y = JKScreenH - 100
            
        }) { (_) in
            
            //定时器，使用该定时器画出icon
            if self.link == nil {
                
                self.link = CADisplayLink(target: self, selector: #selector(JKLaunchViewController.drawIcon))
            }
            
            self.link?.add(to: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
        }
        
        //开始设置动画
        UIView.animate(withDuration: TimeInterval(animationDuration)) {
            
            self.launchImageView.transform = CGAffineTransform(scaleX: self.self.scale, y: self.scale)
        }
        
        perform(#selector(JKLaunchViewController.changeToMainViewController), with: nil, afterDelay: TimeInterval(animationDuration))
    }
    
    @objc private func drawIcon() {
        
        iconView.progress = self.iconView.progress + 1/60.0;
        
        if iconView.progress >= 1.0 {
            iconImgView.image = UIImage(named: "AD_Icon_iOS7")
            iconView.isHidden = true
        }
        
        if (iconView.progress >= animationDuration) {
            
            changeToMainViewController()
            
            link?.invalidate()
            link = nil
        }
    }
    
    
    //动画显示完毕后,把页面跳转到主视图
    @objc private func changeToMainViewController() {
        
        UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.fade)
        
        link?.invalidate()
        link = nil
        
        //    [[NetworkingTool sharedNetworkingTool].tasks makeObjectsPerformSelector:@selector(cancel)];
        //    [[SDWebImageManager sharedManager] cancelAll];
        
        let mainVc = JKMainViewController()
        mainVc.jumpTo = jumpTo
        
        //跳转页面
        //本打算先将控制器modal出来，再设置为根控制器，但是这样的话，启动图片控制器无法销毁~~
        //所以直接替换根控制器
        UIApplication.shared.keyWindow?.rootViewController = mainVc
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


class IconView: UIView {
    
    var progress : CGFloat = 0.0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        let center = CGPoint(x: rect.size.width * 0.5, y: rect.size.height * 0.5)
        
        //以下数字都是慢慢微调出来的~~
        let radius = rect.size.width > rect.size.height ? rect.size.height * 0.265 : rect.size.width * 0.265
        let startA = CGFloat.pi / 2//Double.pi / 2
        let endA = startA + progress * (CGFloat.pi / 2) * 2.9
        
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startA, endAngle: endA, clockwise: true)
        
        path.lineWidth = 5
        path.lineCapStyle = CGLineCap.round
        path.lineJoinStyle = CGLineJoin.round
        
        UIColor(red: 190/255.0, green: 190/255.0, blue: 200/255.0, alpha: 1).set()
        
        path.stroke()
    }
}
